import {simpleGet} from "../../utils/request";
import {pageInit} from "../../utils/interceptor";
import {commonShareAppMessage, commonShareTimeline} from "../../utils/share";
import {getLoginUser} from "../../service/user";

const app = getApp()
const global = app.globalData

Page(pageInit({
    data: {
        nav:{
            title:'首页',
            opacity:0,
            color:"#fff",
            background:"#FA084B"
        },
        imageUrl:global.imageUrl,
        page:null,
        images:[`${global.imageUrl}/user/test_banner.png`],
        loading:true,
        user:null,
    },
    onLoad: function (options) {

        (async () => {

            const {page} = await simpleGet("/index/index")
            const user = await getLoginUser(true)

            console.log("page", page)

            this.setData({
                loading:false,
                page,
                user,
                [`nav.background`]:page.navbar_background,
                [`nav.color`]:page.title_color
            })

        })()

    },

    searchSub(e){
        wx.navigateTo({
            url:`/pages/goods/list?key=${e.detail.value}`
        })
    },

    /**
     * 转发
     */
    onShareAppMessage: function (res) {
        const {user} = this.data
        let title = global.site?.title
        if (user && user.storeManager){
            title = user.storeManager.title
        }
        return commonShareAppMessage(user, {
            title: title,
            path: '/pages/index/index',
            params: {
            }
        })
    },
    /**
     * 分享朋友圈
     */
    onShareTimeline: function () {
        const {user} = this.data
        let title = global.site?.title
        if (user && user.storeManager){
            title = user.storeManager.title
        }
        return commonShareTimeline(user, {
            title: title,
            params: {
            }
        })
    },
}));
